/*
 * File:   627.cpp
 * Author: xerxes
 *
 * Created on September 17, 2011, 9:45 PM
 *
 * I have not failed, I have just found 10000 ways that won't work.
 */

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define foR(i,st,ed) for(int i = st ; i < ed ; ++i )
#define fo(i,ed) foR( i , 0, ed )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

inline void wait(double seconds) {
    double endtime = clock()+(seconds * CLOCKS_PER_SEC);
    while (clock() < endtime) {
        ;
    }
}

int n;
char buff[5000];

inline int toI(string ss) {
    int x;
    sscanf(ss.c_str(), "%d", &x);
    return x;
}

inline vector<int> split() {
    string str = (string) buff;
    stringstream ss(str.substr(1));
    VI vi;
    for (string tmp = ""; getline(ss, tmp, ',');) {
        if (LN(tmp) < 1)continue;
        vi.pb(toI(tmp));
    }
    return vi;

}

vector< VI > path;

inline void Read() {
    path.assign(n + 5, VI());

    fo(i, n) {
        int id;
        scanf("%d%s", &id, buff);
        //cout<<"FROM : "<<id<<"  = ";
        vector<int> vi = split();
        for (int j = 0, k = SZ(vi); j < k; ++j) {
            //cout<<vi[j]<<" ";
            path[id].pb(vi[j]);
        }
        //cout<<"\n";
    }
}

inline void Print(VI &v) {
    cout << v[0];
    for (int i = 1, j = SZ(v); i < j; ++i) {
        cout << ' ' << v[i];
    }
    cout << "\n";
}

int vis[ 500 ];

inline void bfs(int x, int y) {
    SET(vis, 0);
    queue< pair<int, VI > > q;
    q.push(mk(x, VI(1, x)));
    vis[x] = 1;

    while (q.empty() == false) {
        int father = q.front().f;
        VI cur = q.front().s;
        q.pop();

        if (father == y) {
            Print(cur);
            return;
        }
        for (int i = 0, j = SZ(path[father]); i < j; ++i) {
            int child = path[father][i];
            if (vis[child])continue;
            vis[child] = 1;
            VI nw = cur;
            nw.pb(child);
            q.push(mk(child, nw));
        }
    }




    cout << "connection impossible\n";
}

inline void Proc() {
    int m;
    CI(m);
    cout << "-----\n";
    while (m--) {
        int x, y;
        CI(x);
        CI(y);
        bfs(x, y);
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in", "rt", stdin);
#endif
    while (scanf("%d", &n) == 1) {
        Read();
        Proc();
    }

    return 0;
}


